//
//  taskFooterView.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//
import Foundation
import UIKit
import SnapKit
import Alamofire
class TaskFooterView:UICollectionReusableView{
    var describeLable:UILabel!
    var footerButton:MyButton!
    var upNumber:TaskListFootder!
    let api = "/tasklist-footer"
    //初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadData(userId: 1)
//        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        //标签设置
        describeLable = UILabel()
        describeLable.text = "今日可升级得" + self.upNumber.upNumber + "个解锁次数，再做1个任务"
        describeLable.textColor = .black
        describeLable.textAlignment = .center
        describeLable.font = UIFont.systemFont(ofSize: 15)
        //  标签部分设置颜色
        let attributedString = NSMutableAttributedString(string: describeLable.text!)
        attributedString.addAttribute(
            .foregroundColor,
            value: UIColor.systemOrange.withAlphaComponent(0.9),
            range: NSRange(location: 2, length: 9 + self.upNumber.upNumber.count)
        )
        describeLable.attributedText = attributedString
        
        
        //按钮设置
        footerButton = MyButton()
        footerButton.setImage(UIImage(systemName: "chevron.forward"), for: .normal)
        footerButton.tintColor = .black
        //将标签和按钮放在stack中
        let containerView = UIView()
        containerView.addSubview(describeLable)
        containerView.addSubview(footerButton)
        //将view放入footer
        self.addSubview(containerView)
        
        //组件布局
        describeLable.snp.makeConstraints { make in
            make.height.equalTo(20)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        footerButton.snp.makeConstraints { make in
            make.left.equalTo(describeLable.snp.right).inset(0)
            make.height.width.equalTo(15)
            make.centerY.equalToSuperview()
        }

        
        containerView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15))
        }
    }
    func loadData(userId:Int){
        let url = AllUrl.LH_URL + api + "?userId=\(userId)"
        
        AF.request(url,method: .get)
            .validate(statusCode: 200..<300)
            .responseDecodable(of:TaskListFootder.self){ response in
                switch response.result{
                case .success(let footer):
                    DispatchQueue.main.async {
                        self.upNumber = footer
                        self.setupUI()
                    }
                case .failure(let error):
                    print("错误: \(error.localizedDescription)")
                }
            }
    }
}
